import { Injectable } from '@angular/core';
import { AppConfig } from "../config";
import { Http, Headers } from "@angular/http";
import { RunStop } from "../entity/run-stop.model";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class RunStopsService {
  private readonly param = "runStops";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) { }
  getRunStops(): Promise<RunStop[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.json().data as RunStop[])
      .catch(this.handleError);
  }
  getRunStopsByModelIds(modelIds:number[]){
    const apiUrl = `${this.url}/modelIds?items=${modelIds}`;
    //const apiUrl = `http://192.168.15.110:8989/webapi/v1/runStops/modelIds?items=${modelIds}`;
    return this.http.get(apiUrl)
      .toPromise()
      .then(response => response.json().data as RunStop[])
      .catch(this.handleError);

  }
  getRunStopsBySchemeId(schemeId: number): Promise<RunStop[]> {
    const url = `${this.url}/schemeId/${schemeId}`;
    return this.http.get(url)
      .toPromise()
      .then(response => response.json().data as RunStop[])
      .catch(this.handleError);
  }
  create(runStop: RunStop): Promise<RunStop> {
    return this.http
      .post(this.url, JSON.stringify(runStop))
      .toPromise()
      .then(res => res.json().data)
      .catch(this.handleError);
  }
  update(runStop: RunStop): Promise<boolean> {
    const url = `${this.url}`;
    return this.http
      .put(url, JSON.stringify(runStop))
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return true;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }
  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  batchDelete(keyIds: number[]): Promise<any> {

    const apiUrl = `${this.url}?items=${keyIds}`;
    // const apiUrl = `http://192.168.15.150:8585/webapi/v1/runStops?items=${keyIds}`;
    
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return result;
            case "-1":
              throw new Error(result.message);
            case "8001":
              return result;
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }
  runStopCodeService(runStopCode: string,schemeId:number, runStopId: number) {
    const url = `${this.url}/runStopCode/${runStopCode}/schemeId/${schemeId}/runStopId/${runStopId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }
    }).catch(this.handleError);
  }
  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }
}



// WEBPACK FOOTER //
// ./src/app/service/run-stops.service.ts